

namespace Stee.CAP8.MessageBus.Kafka.Models
{
    using System.Collections.Generic;
    using Stee.CAP8.MessageBus.Kafka.Utility;

    /// <summary>
    /// Kafka topic details, Partition count
    /// </summary>
    internal class TopicDetail
    {
        /// <summary>
        /// Gets the topic name
        /// </summary>
        internal string TopicName { get; }

        /// <summary>
        /// Gets the replication factor
        /// </summary>
        internal short ReplicationFactor { get; }

        /// <summary>
        /// Collection of unique topic partition IDs
        /// </summary>
        private readonly HashSet<int> partitionIDs = new HashSet<int>();


        /// <summary>
        /// Initializes a new instance of the TopicDetail class
        /// </summary>
        /// <param name="topicName"></param>
        internal TopicDetail(string topicName)
        {
            this.TopicName = ValidateHelper.CheckString(topicName, excpMsg: "Topic name cannot be NULL or empty.");
        }


        /// <summary>
        /// Add new partition ID
        /// </summary>
        /// <param name="id">Partition ID to add</param>
        /// <returns>Return TRUE if added successfully else FALSE</returns>
        internal bool AddPartitionID(int id)
        {
            return this.partitionIDs.Add(id);
        }

        /// <summary>
        /// Remove existing partition ID
        /// </summary>
        /// <param name="id">Partition ID to remove</param>
        /// <returns>Return TRUE if removed successfully else FALSE</returns>
        internal bool RemovePartitionID(int id)
        {
            return this.partitionIDs.Remove(id);
        }

        /// <summary>
        /// Check if partition ID exist
        /// </summary>
        /// <param name="id">Partition ID to check</param>
        /// <returns>Return TRUE if exist else FALSE</returns>
        internal bool IsPartitionIDExist(int id)
        {
            return this.partitionIDs.Contains(id);
        }

        /// <summary>
        /// Get all partition IDs
        /// </summary>
        /// <returns>Return all partitions as new hashset instance</returns>
        internal HashSet<int> GetAllPartitions()
        {
            return new HashSet<int>(this.partitionIDs);
        }
    }
}
